package com.weavernorth.action.application;


import com.engine.kq.biz.KQLeaveRulesBiz;
import com.engine.kq.biz.KQLeaveRulesComInfo;
import com.engine.kq.biz.KQLeaveRulesDetailComInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * 加班流程节点后更新调休有效日期
 */
public class UpdateEffectiveDateAction implements Action {
    private static Log log = LogFactory.getLog(UpdateEffectiveDateAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        //当前requestid
        String strRequestid = requestInfo.getRequestid();
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsDelay = new RecordSet();
        RecordSet rsMax = new RecordSet();
        //当前日期
        String strCurrent = DateUtil.getCurrentDate();
        //假期规则缓存类
        KQLeaveRulesDetailComInfo detailComInfo = new KQLeaveRulesDetailComInfo();
        //假期类型的缓存类
        KQLeaveRulesComInfo rulesComInfo = new KQLeaveRulesComInfo();
        //[调休]的假期类型的ID
        String leaveRulesId = "";
        //找到[调休]的假期类型ID
        rulesComInfo.setTofirstRow();
        while (rulesComInfo.next()) {
            if (KQLeaveRulesBiz.isTiaoXiu(rulesComInfo.getId())) {
                leaveRulesId = rulesComInfo.getId();
                break;
            }
        }


        //当前表名
        //String strTable = requestInfo.getRequestManager().getBillTableName();
        RecordSet rsTable=new RecordSet();
        String strTable ="";
        rsTable.execute("select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='"+strRequestid+"') and isbill=1)");
        if(rsTable.next()){
            strTable=rsTable.getString("tablename");
        }
        //查询当前人
        String strSql = "select fmd.detail_fromDate,hr.subcompanyid1,fmd.detail_resourceId,hr.departmentid  from " + strTable + " fm," + strTable + "_dt1 fmd,hrmresource hr  where hr.id=fmd.detail_resourceId and fm.id=fmd.mainid  and fm.requestid=?";
        log.info("查询当前加班所有数据:" + strSql);
        rs.executeQuery(strSql, strRequestid);
        while (rs.next()) {
            //姓名
            String strUser = Util.null2String(rs.getString("detail_resourceId"));
            log.info("加班申请中明细表人员id:" + strUser);
            //人员所属部门
            String strDepartmentid = Util.null2String(rs.getString("departmentid"));
            //日期
            String strDetail_fromDate = Util.null2String(rs.getString("detail_fromDate"));
            log.info("加班日期:"+strDetail_fromDate);
            //分部
            String strSubcompanyid = Util.null2String(rs.getString("subcompanyid1"));
            //有效期规则：0-不限制、1-按自然年失效、2-按入职日期起12个月、3-自定义次年失效日期、4-按天数失效
            int validityRule = Util.getIntValue(detailComInfo.getValidityRule(leaveRulesId, strSubcompanyid), 0);
            log.info("当前有效期规则:" + validityRule);
            //根据人员查找所属部门和延长调休时长天数
            if (validityRule == 4) {
                String delaySql = "select  dxsxts from uf_txzdysxts where ','||pzbmfw||',' like '%," + strDepartmentid + ",%'";
                log.info("查询延长有效天数:" + delaySql);
                rsDelay.executeQuery(delaySql);
                if (rsDelay.next()) {
                    int intDelay = Util.getIntValue(rsDelay.getString("dxsxts"), 0);
                    log.info("延长的天数:" + intDelay);
                    String expirationDate = DateUtil.addDate(strCurrent, intDelay);
                    log.info("延长后的失效日期:" + expirationDate);
                    //更新最大id的失效日期
                    String strUpsql = "update kq_balanceOfLeave set expirationdate='" + expirationDate + "' where resourceId=? and effectivedate=?";
                    rsMax.executeQuery(strUpsql, strUser, strDetail_fromDate);

                }
            }
        }

        return SUCCESS;

    }

}